package com.gmail.linuxpro13.iblocklog.events;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import java.util.Calendar;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;

public class BlockPlace implements Listener {

	File blockplace = new File("iblocklog/BlockPlace.log");
	
	@EventHandler(priority=EventPriority.NORMAL)
	public void onBlockPlace(BlockPlaceEvent event) {
		Block block = event.getBlock();
		Material material = event.getBlock().getType();
		Player player = event.getPlayer();
		Calendar date = Calendar.getInstance();
		
		try {
			if(blockplace.exists()) {
				FileWriter writer = new FileWriter(blockplace);
				
				writer.write("\r\n");
				writer.write(player.getName() + " placed block " + block.getType() + "(" + material.getId() + ") at " + date.getTime().toString());
				writer.close();
			} else {
				blockplace.mkdirs();
				blockplace.mkdir();
				blockplace.createNewFile();
				
				FileWriter writer = new FileWriter(blockplace);
				
				writer.write("\r\n");
				writer.write(player.getName() + " destroyed block " + block.getType() + "(" + material.getId() + ") at " + date.getTime().toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
